package com.txunda.zhuohired.aty.user.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ants.theantsgo.tools.RegexUtils;
import com.ants.theantsgo.util.JSONUtils;
import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.http.pst.InfoPst;
import com.txunda.zhuohired.util.IDCardUtils;

import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/8 14:44
 * 功能描述：添加银行卡
 * 联系方式：jiahuanchen@126.com
 */

public class AddBankAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 卡类型
     */
    @ViewInject(R.id.tv_add_bank_type)
    private TextView tv_add_bank_type;
    /**
     * 持卡人姓名
     */
    @ViewInject(R.id.et_add_bank_name)
    private EditText et_add_bank_name;
    /**
     * 银行卡号
     */
    @ViewInject(R.id.et_add_bank_num)
    private EditText et_add_bank_num;
    /**
     * 身份证号
     */
    @ViewInject(R.id.et_add_bank_id_card)
    private EditText et_add_bank_id_card;
    /**
     * 预留手机号
     */
    @ViewInject(R.id.et_add_bank_phone)
    private EditText et_add_bank_phone;

    private int REQUESTCODE = 1;
    private InfoPst infoPst;
    private String bank_id = "";
    private String name = "";//持卡人姓名
    private String bank_num = "";//银行卡号
    private String id_card = "";//身份证号
    private String phone = "";//手机号
    private String bank_type = "";//银行卡类型
    private String token = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("添加银行卡");
    }

    @Override
    @OnClick({R.id.tv_add_bank_sure, R.id.ll_add_bank_type})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_add_bank_sure://确认添加
                name = et_add_bank_name.getText().toString().trim();
                bank_num = et_add_bank_num.getText().toString().trim();
                bank_type = tv_add_bank_type.getText().toString().trim();
                id_card = et_add_bank_id_card.getText().toString().trim();
                phone = et_add_bank_phone.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    showErrorTip("请输入持卡人姓名");
                    return;
                }
                if (TextUtils.isEmpty(bank_num)) {
                    showErrorTip("请输入银行卡号");
                    return;
                }
                if (TextUtils.isEmpty(bank_type)) {
                    showErrorTip("请选择银行卡类型");
                    return;
                }
                if (TextUtils.isEmpty(id_card)) {
                    showErrorTip("请输入身份证号");
                    return;
                }
                if (!RegexUtils.checkMobile(phone)) {
                    showErrorTip("请检查手机号");
                    return;
                }
                try {
                    if (!"".equals(IDCardUtils.IDCardValidate(id_card))) {
                        showErrorTip("请检查您的身份证号");
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (checkBankCard(bank_num) == false) {
                    showErrorTip("请检查您的银行卡号");
                    break;
                }
                L.e("==========",bank_type);
                infoPst.bindBank(token, name, bank_num, bank_id, phone, id_card);
                //
                break;
            case R.id.ll_add_bank_type://卡类型
                startActivityForResult(BankTypeAty.class, null, REQUESTCODE);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_add_bank;
    }

    @Override
    protected void initialized() {
        token = application.getUserInfo().get("data");
        infoPst = new InfoPst(this);
    }

    @Override
    protected void requestData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (requestCode == REQUESTCODE) {//银行卡类型
                bank_id = data.getStringExtra("bank_id");
                tv_add_bank_type.setText(data.getStringExtra("bank_name"));
            }
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("bindBank")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            finish();
            startActivity(AddBankSureAty.class, null);
            return;
        }
    }

    /**
     * 判断是否是银行卡号
     *
     * @param cardNo
     * @return
     * @author WJ
     */
    public static boolean checkBankCard(String cardNo) {
        char bit = getBankCardCheckCode(cardNo
                .substring(0, cardNo.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardNo.charAt(cardNo.length() - 1) == bit;

    }

    private static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null
                || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }
}
